package org.jetnuke.modules.admin.authors;

import org.apache.log4j.Logger;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.jforum.entities.User;
import net.jforum.repository.SecurityRepository;
import net.jforum.security.PermissionControl;

import org.apache.velocity.VelocityContext;
import org.jetnuke.common.ActionForward;
import org.jetnuke.common.AdminInfo;
import org.jetnuke.common.IAction;
import org.jetnuke.common.IAdminInfo;
import org.jetnuke.common.IModuleInfo;
import org.jetnuke.common.IUserInfo;
import org.jetnuke.dao.DataAccessDriver;

public class AuthorEdit implements IAction {
	/**
	 * Logger for this class
	 */
	private static final Logger logger = Logger.getLogger(AuthorEdit.class);

	public void setAid(int aid) {
		this.aid = aid;
	}

	int aid;

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.jetnuke.common.IAction#exec(javax.servlet.http.HttpServletRequest,
	 *      javax.servlet.http.HttpServletResponse,
	 *      org.apache.velocity.VelocityContext, org.jetnuke.common.IModuleInfo,
	 *      java.lang.String, int)
	 */
	public ActionForward exec(HttpServletRequest request,
			HttpServletResponse response, VelocityContext vc, IModuleInfo info,
			String op, IUserInfo ui, IAdminInfo ai) throws Exception {
		if (logger.isDebugEnabled()) {
			logger
					.debug("exec(HttpServletRequest, HttpServletResponse, VelocityContext, IModuleInfo, String, IUserInfo, IAdminInfo) - start"); //$NON-NLS-1$
		}

		if (ai == null || !ai.isSuperUser())
			return null;// Error;

		PermissionControl pc = SecurityRepository.get(aid);

		IAdminInfo iAdminInfo = new AdminInfo(pc);

		vc.put("RADMINARTICLE", new Boolean(iAdminInfo.isAdminArticle()));
		vc.put("RADMINCONTENT", new Boolean(iAdminInfo.isAdminContent()));
		vc.put("RADMINDOWNLOAD", new Boolean(iAdminInfo.isAdminDownload()));
		vc.put("RADMINENCY", new Boolean(iAdminInfo.isAdminEncy()));
		vc.put("RADMINEPHEM", new Boolean(iAdminInfo.isAdminEphem()));
		vc.put("RADMINFAQ", new Boolean(iAdminInfo.isAdminFaq()));
		// vc.put("_admin_forum", new Boolean(iAdminInfo.isAdminForum()));
		vc.put("RADMINLINK", new Boolean(iAdminInfo.isAdminLink()));
		vc.put("RADMINNEWSLETTER", new Boolean(iAdminInfo.isAdminNewsLetter()));
		vc.put("RADMINREVIEWS", new Boolean(iAdminInfo.isAdminReviews()));
		vc.put("RADMINSECTION", new Boolean(iAdminInfo.isAdminSection()));
		vc.put("RADMINSURVEY", new Boolean(iAdminInfo.isAdminSurvey()));
		vc.put("RADMINTOPIC", new Boolean(iAdminInfo.isAdminTopic()));
		vc.put("RADMINUSER", new Boolean(iAdminInfo.isAdminUser()));
		vc.put("RADMINSUPER", new Boolean(iAdminInfo.isSuperUser()));

		User us = DataAccessDriver.getInst().newUserDAO().selectById(aid);

		// authorsPeer
		// .doSelect(new Criteria().add(authorsPeer.AID, aid));
		// Author au = (Author) ls.get(0);
		vc.put("author", us);
		ActionForward returnActionForward = new ActionForward(op, info);
		if (logger.isDebugEnabled()) {
			logger
					.debug("exec(HttpServletRequest, HttpServletResponse, VelocityContext, IModuleInfo, String, IUserInfo, IAdminInfo) - end"); //$NON-NLS-1$
		}
		return returnActionForward;
	}

}
